<?php

namespace app\admin\controller\course;

use app\common\controller\Backend;
use app\common\model\Classes;
use app\common\model\CourseDetail;
use app\common\model\Schedule;
use app\common\model\School;
use GuzzleHttp\Client;
use think\Collection;
use think\Db;
use think\Exception;
use think\exception\PDOException;
use think\exception\ValidateException;

/**
 * 教师课表
 *
 * @package app\admin\controller
 */
class Teacher extends Backend
{
    protected $noNeedRight = ['searchList', 'cxSelect'];
    protected $schoolLimit = true;
    protected $typeData = [];
    protected $schoolData = [];

    public function _initialize()
    {
        parent::_initialize();

        /*$this->typeData = [
            '1' => '行政班级',
            '2' => '走班班级',
        ];
        $this->view->assign('typeData', $this->typeData);*/

        $result = School::whereIn('id', $this->auth->getSchoolIds())->select();
        foreach ($result as $key => $value) {
            $this->schoolData[$value['id']] = $value['name'];
        }
        $this->view->assign('schoolData', $this->schoolData);//学校
    }

    public function index()
    {
        if ($this->request->isAjax()) {
            $school_id = $this->request->request('school_id');
            $teacher_id = $this->request->request('teacher_id');
            $semester_id = $this->request->request('semester_id');
            $semester_num = $this->request->request('semester_num');

            //教师任课
            $teaches = \app\common\model\Teach::alias('teach')
                ->join('__SUBJECT__ subject', 'subject.id = teach.subject_id')
                ->where('teach.school_id', $school_id)
                ->where('teach.teacher_id', $teacher_id)
                ->column('teach.id,teach.type,subject.name subject_name', 'teach.id');

            $num = 1;
            foreach ($teaches as $key => $value) {
                $teaches[$key]['num'] = $num > 8 ? $num % 8 : $num;
                $num++;
            }

            $teach_ids = array_keys($teaches);
            $course_details = [];
            $list = CourseDetail::whereIn('teach_id', $teach_ids)
                ->where('semester_id', $semester_id)
                ->where('semester_num', $semester_num)
                ->select();

            if ($list) {
                $class_ids = array_column(Collection($list)->toArray(), 'class_id');
                $classes = Classes::alias('class')
                    ->join('__GRADE__ grade', 'grade.id = class.grade_id')
                    ->whereIn('class.id', $class_ids)
                    ->column('class.id,class.name class_name,grade.name grade_name', 'class.id');

                foreach ($list as $key => $value) {
                    if (isset($teaches[$value['teach_id']]) && isset($classes[$value['class_id']])) {
                        $course_details[$value['number']][$value['day']] = [
                            'class_name' => $classes[$value['class_id']]['class_name'],
                            'grade_name' => $classes[$value['class_id']]['grade_name'],
                            'subject_name' => $teaches[$value['teach_id']]['subject_name'],
                            'num' => $teaches[$value['teach_id']]['num'],
                        ];
                    }
                }
            }

            //所有课节
            $class_days = [];
            $list = \app\common\model\ClassDay::where('school_id', $this->auth->school_id)
                ->where('school_id', $school_id)
                ->select();
            foreach ($list as $key => $class_day) {
                if (isset($course_details[$class_day['number']]) && isset($course_details[$class_day['number']][$class_day['day']])) {
                    $temp = $course_details[$class_day['number']][$class_day['day']];
                    $temp['start_time'] = $class_day['start_time_text'];
                    $temp['end_time'] = $class_day['end_time_text'];
                    $class_days[$class_day['number']][] = $temp;
                } else {
                    $class_days[$class_day['number']][] = [];
                }
            }
            //$class_days = array_values($class_days);

            $this->success('', '', $class_days);
        }

        return $this->view->fetch();

    }

}
